package com.ruoyi.card.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrSplitter;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.google.common.base.Joiner;
import com.ruoyi.card.domain.CardSecret;
import com.ruoyi.card.service.ICardSecretService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.alone.LonelyData;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

/**
 * 卡密管理Controller
 *
 * @author shan
 * @date 2022-06-08
 */
@Controller
@RequestMapping("/cardSecret/api")
public class CardSecretApiController extends BaseController {

    @Autowired
    private ICardSecretService cardSecretService;

    @ApiOperation("设置卡密使用状态")
    @GetMapping("/setUsedState")
    public LonelyData<Boolean> setCardUseState(@RequestParam(value = "cardId") String cardId, @RequestParam(value = "ts") Long ts, @RequestParam(value = "ct") String ct,@RequestParam(value = "op") Integer op){
        LonelyData<Boolean> lonelyData = new LonelyData<>();
        lonelyData.setData(false);
        long currentTs = System.currentTimeMillis() / 1000L;
        if (currentTs - ts > 60) {
            lonelyData.setMsg("超时");
            return lonelyData;
        }
        int lineCount = StringUtils.countMatches(cardId, "-");
        if (lineCount != 4) {
            lonelyData.setMsg("卡密格式不正确");
            return lonelyData;
        }
        List<String> split = StrSplitter.split(cardId, ',', 0, true, true);
        String validateStr = Joiner.on("_").join(split.get(0), split.get(split.size() - 1),op, ts);
        String checkedMd5 = SecureUtil.md5(validateStr);
        if (!checkedMd5.equals(ct)) {
            lonelyData.setMsg("签名不正确");
            return lonelyData;
        }
        CardSecret cardInfo= cardSecretService.selectGoodCardSecret(cardId);
        if(cardInfo.getCardState()==2){
            lonelyData.setMsg("卡密停用");
            return lonelyData;
        }
        if(cardInfo.getExpireDatetime().before(new Date())){
            lonelyData.setMsg("卡密停用");
            return lonelyData;
        }
        cardSecretService.setUseCard(cardId,op);
        lonelyData.setData(true);
        lonelyData.setMsg("设置成功！");
        return lonelyData;
    }




    @ApiOperation("检查卡密状态")
    @GetMapping("/cardCheck")
    public LonelyData<Boolean> getCardState(@RequestParam(value = "cardId") String cardId, @RequestParam(value = "ts") Long ts, @RequestParam(value = "ct") String ct) {
        LonelyData<Boolean> lonelyData = new LonelyData<>();
        lonelyData.setData(false);
        long currentTs = System.currentTimeMillis() / 1000L;
        if (currentTs - ts > 60) {
            lonelyData.setMsg("超时");
            return lonelyData;
        }
        int lineCount = StringUtils.countMatches(cardId, "-");
        if (lineCount != 4) {
            lonelyData.setMsg("卡密格式不正确");
            return lonelyData;
        }
        List<String> split = StrSplitter.split(cardId, ',', 0, true, true);
        String validateStr = Joiner.on("_").join(split.get(0), split.get(split.size() - 1), ts);
        String checkedMd5 = SecureUtil.md5(validateStr);
        if (!checkedMd5.equals(ct)) {
            lonelyData.setMsg("签名不正确");
            return lonelyData;
        }
        CardSecret cardInfo= cardSecretService.selectGoodCardSecret(cardId);
        if(cardInfo.getCardState()==2){
            lonelyData.setMsg("卡密停用");
            return lonelyData;
        }
        if(cardInfo.getExpireDatetime().before(new Date())){
            lonelyData.setMsg("卡密停用");
            return lonelyData;
        }
        lonelyData.setData(true);
        lonelyData.setMsg("卡密正常");
        return lonelyData;
    }

}
